package util;

import DateTools.DateUtil;
import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by bowen on 2016/9/6.
 */
public class ImageUploadUtil {

    public static void GenerateImage(String filePath,String imageType,String imgStr) throws IOException {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytes = decoder.decodeBuffer(imgStr);
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        RenderedImage image = ImageIO.read(bais);
        File file = new File(filePath);
        if(!file.exists()){
            file.mkdirs();
            file.createNewFile();
        }
        ImageIO.write(image, imageType, file);
    }



    /**
     * 添加文字水印
     * @param targetImg 目标图片路径，如：C://myPictrue//1.jpg
     */
    public static void pressText(String targetImg,String address,String imageType) {
        try {
            File file = new File(targetImg);
            Image image = ImageIO.read(file);
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);
            g.setFont(new Font("微软雅黑",Font.BOLD,60));
            g.setColor(Color.WHITE);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,1));
            Calendar ca = Calendar.getInstance();
            String time =ca.get(Calendar.HOUR_OF_DAY)+":"+ca.get(Calendar.MINUTE);
            int width_1 = 60 * getLength(time);
            int height_1 = 60;
            int widthDiff = width - width_1;
            int heightDiff = height - height_1;
            g.drawString(time,widthDiff/2,heightDiff-40);
            g.setFont(new Font("微软雅黑",Font.BOLD,30));
            String date = DateUtil.DateToString(new Date(), "yyyy-MM-dd");
            SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
            String dataStr = date+" "+dateFm.format(new Date())+" "+address;
            widthDiff =width - 30 * getLength(dataStr);
            g.drawString(dataStr,widthDiff/2,heightDiff);
            g.dispose();
            ImageIO.write(bufferedImage,imageType, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取字符长度，一个汉字作为 1 个字符, 一个英文字母作为 0.5 个字符
     * @param text
     * @return 字符长度，如：text="中国",返回 2；text="test",返回 2；text="中国ABC",返回 4.
     */
    public static int getLength(String text) {
        int textLength = text.length();
        int length = textLength;
        for (int i = 0; i < textLength; i++) {
            if (String.valueOf(text.charAt(i)).getBytes().length > 1) {
                length++;
            }
        }
        return (length % 2 == 0) ? length / 2 : length / 2 + 1;
    }


}
